package com.atguigu.gmall.product.controller.admin;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/***
 * 平台属性值表的Controller层
 */
@RequestMapping("/attrValue")
@RestController
public class BaseAttrValueController {

    @Autowired
    private BaseAttrValueService baseAttrValueService;

    /**
     * 条件分页查询
     * @param baseAttrValue
     * @param pageNum
     * @param size
     * @return
     */
    @GetMapping("/search/{pageNum}/{size}")
    public Result search(@RequestBody BaseAttrValue baseAttrValue,
                         @PathVariable("pageNum") Integer pageNum,
                         @PathVariable("size") Integer size){
        IPage<BaseAttrValue> search = baseAttrValueService.search(baseAttrValue, pageNum, size);

        return Result.ok(search);
    }

    /**
     * 条件查询
     * @param baseAttrValue
     * @return
     */
    @GetMapping("/search")
    public Result search(@RequestBody BaseAttrValue baseAttrValue){
        List<BaseAttrValue> attrValues = baseAttrValueService.search(baseAttrValue);
        return Result.ok(attrValues );
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param size
     * @return
     */
    @GetMapping("/page/{pageNum}/{size}")
    public Result page(@PathVariable("pageNum") Integer pageNum,
                       @PathVariable("size") Integer size) {
        IPage<BaseAttrValue> page = baseAttrValueService.page(pageNum, size);

        return Result.ok(page);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/removeById/{id}")
    public Result removeById(@PathVariable("id") Long id) {
        baseAttrValueService.removeById(id);
        return Result.ok();
    }

    /**
     * 根据id修改
     *
     * @param baseAttrValue
     * @return
     */
    @PutMapping("/edit")
    public Result edit(@RequestBody BaseAttrValue baseAttrValue) {
        baseAttrValueService.edit(baseAttrValue);
        return Result.ok();
    }

    /**
     * 新增数据
     *
     * @param baseAttrValue
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody BaseAttrValue baseAttrValue) {
        baseAttrValueService.save(baseAttrValue);
        return Result.ok();
    }

    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable("id") Long id) {
        BaseAttrValue attrValue = baseAttrValueService.getById(id);

        return Result.ok(attrValue);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        return Result.ok(baseAttrValueService.list());
    }

}
